//
//  MyInfoViewController.m
//  fitnessmanage
//
//  Created by wujian on 2017/12/15.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "MyInfoViewController.h"
#import "MyInfoView.h"
#import "ZLPhotoPickerViewController.h"
#import "UIImage+ZLPhotoLib.h"
#import "ZLPhotoPickerBrowserPhoto.h"
#import "MOFSPickerManager.h"
#import "CZQLongViewController.h"
#import "CZQEidtJobViewController.h"
@interface MyInfoViewController ()

@property(nonatomic,weak)MyInfoView *myInfoView;

@property (nonatomic , strong) ZLPhotoPickerBrowserPhoto *photo;

/**
 存放擅长数据
 */
@property(nonatomic,strong)NSMutableArray *longArrM;

@end

@implementation MyInfoViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    
    
    
}

- (void)viewDidLoad {
    
    [super viewDidLoad];

    self.title = @"编辑个人资料";
    
    self.resumeModel = [CZQResumeModel new];
    
    //擅长
    if (self.userAccount.FavTags.length > 0) {
        
        self.resumeModel.FavTags = self.userAccount.FavTags;
    }
    
    
    [self createSubView];
}

-(NSMutableArray *)longArrM
{
    if (!_longArrM) {
        _longArrM = [NSMutableArray array];
    }
    return _longArrM;
}

#pragma mark 创建子视图
-(void)createSubView{
    
    [self.myInfoView removeFromSuperview];
    
    MyInfoView *myInfoView = [[MyInfoView alloc] init];
    
    //头像选择
    [myInfoView.headBtn addTarget:self action:@selector(logoSelect:)];
    
    NSString *urlStr =  [NSString stringWithFormat:@"%@%@",self.userAccount.Pic ,mid_image];
    urlStr=[urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [myInfoView.headBtn sd_setImageWithURL:[NSURL URLWithString:urlStr] forState:UIControlStateNormal placeholderImage:PlaceHold];
    
    
    //性别
    [myInfoView.sexBtnView.choseBtn addTarget:self action:@selector(showSex)];
    
    myInfoView.sexBtnView.choseLabel.text = [self.userAccount.Sex intValue] == 0? @"男" : @"女";
    myInfoView.sexBtnView.choseLabel.textColor = TFColor;
    
    
    //名字
    myInfoView.nameInputView.titleTF.text = self.userAccount.NickName;
    
    myInfoView.realNameInputView.titleTF.text = self.userAccount.RealName;
    
    //地址选择
    myInfoView.addressBtnView.choseLabel.text = self.userAccount.Address;
    
    [myInfoView.addressBtnView.choseBtn addTarget:self action:@selector(addressSelect)];
    
    [myInfoView.sureBtn addTarget:self action:@selector(sureBtnClick)];
    
    myInfoView.backgroundColor = BackGroundColor;
    
    //微信
    myInfoView.weChatInputView.titleTF.text = self.userAccount.WxAccount;
    
    //备注
    myInfoView.talkView.max = 30;
    myInfoView.talkView.textView.text = self.userAccount.Memo;
    [myInfoView.talkView textViewDidChange:self.userAccount.Memo];
    
    [self.view addSubview:myInfoView];
    
    
    
    myInfoView.longArrM = [self.resumeModel.FavTags componentsSeparatedByString:@","];
    
//    if (self.resumeModel.FavTags.length > 0) {
//
//
//        self.longArrM = self.myInfoView.longArrM;
//
//    }else
//    {
//        myInfoView.longArrM = nil;
//
//        self.longArrM = nil;
//    }
    
    [myInfoView.longBtnView.choseBtn addTarget:self action:@selector(longSelect)];
    
    [myInfoView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.mas_equalTo(self.view);
    }];
    
    //self.userAccount = [UserAccount account];
    
    self.myInfoView = myInfoView;
}

#pragma Logo图片选择
-(void)logoSelect:(UIButton *)photoSelect{
    
    ZLPhotoPickerViewController *pickerVc = [[ZLPhotoPickerViewController alloc] init];
    
    pickerVc.maxCount = 1;
    
    // Jump AssetsVc
    pickerVc.status = PickerViewShowStatusCameraRoll;
    // Recoder Select Assets
    //    pickerVc.selectPickers = self.assets;
    // Filter: PickerPhotoStatusAllVideoAndPhotos, PickerPhotoStatusVideos, PickerPhotoStatusPhotos.
    
    pickerVc.photoStatus = PickerPhotoStatusPhotos;
    // Desc Show Photos, And Suppor Camera
    pickerVc.isShowCamera = YES;
    pickerVc.topShowPhotoPicker = YES;
    
    // CallBack
    __weak typeof(self)weakSelf = self;
    
    pickerVc.callBack = ^(NSArray<ZLPhotoAssets *> *status){
        
        for (ZLPhotoAssets *assets in status) {
            
            ZLPhotoPickerBrowserPhoto *photo = [ZLPhotoPickerBrowserPhoto photoAnyImageObjWith:assets];
            
            photo.asset = assets;
            
            weakSelf.photo = photo;
            
            //拍照
            if ([photo.asset isKindOfClass:[UIImage class]]) {
                
                [weakSelf.myInfoView.headBtn setImage:(UIImage *)photo.asset forState:UIControlStateNormal];
            }
            else{
                
                [weakSelf.myInfoView.headBtn setImage:[assets thumbImage] forState:UIControlStateNormal];
                
            }
            
            [weakSelf uploadImage];
            
            break;
        }
    };
    
    [pickerVc showPickerVc:self];
}

#pragma mark 上传头像
-(void)uploadImage{
    
    if (!self.photo) {
        
        return;
    }
    
    NSData *imageData;
    
    UIImage *image;
    
    if ([self.photo.asset isKindOfClass:[UIImage class]]) {
        
        image = (UIImage *)self.photo.asset;
    }
    else{
        
        image = [self.photo.asset originImage];
    }
    
    imageData  = UIImageJPEGRepresentation(image,0.1);
    
//    [MBProgressHUD showMessage:@"上传中.." toView:self.view];
    
    RAYUploader *upload = [RAYUploader defaultUploader];

    [upload uploadWithData:imageData complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {

        [MBProgressHUD hideHUDForView:self.view];

        if (key) {

            dispatch_async(dispatch_get_main_queue(), ^{

                NSString *picUrl = [NSString stringWithFormat:@"%@%@",QiNiuBase,key];

                self.userAccount.Pic = picUrl;
            });

        }

    }];
    
}

#pragma mark 显示性别
-(void)showSex{
    
    [self.view endEditing:YES];
    
    __weak typeof(self)weakSelf = self;
    
    [[MOFSPickerManager shareManger] showPickerViewWithDataArray:@[@"男",@"女"] tag:67 title:@"选择性别" cancelTitle:@"取消" commitTitle:@"确定" commitBlock:^(NSString *string) {
        
        weakSelf.myInfoView.sexBtnView.choseLabel.text = string;
        
        weakSelf.myInfoView.sexBtnView.choseLabel.textColor = TFColor;
        
        weakSelf.userAccount.Sex = [string isEqualToString:@"男"]?@"0":@"1";
        
    } cancelBlock:^{
        
    }];
}


#pragma mark 显示城市
-(void)addressSelect{
    
    __weak typeof(self)weakSelf = self;
    
    [[MOFSPickerManager shareManger] showMOFSAddressPickerWithDefaultAddress:@"" title:@"选择城市" cancelTitle:@"取消" commitTitle:@"确定" commitBlock:^(NSString *address, NSString *zipcode) {
        
        weakSelf.myInfoView.addressBtnView.choseLabel.text = address;
        
        weakSelf.myInfoView.addressBtnView.choseLabel.textColor = TFColor;
        
        weakSelf.userAccount.Address = address;
        
    } cancelBlock:^{
        
        
    }];
}

/**
 擅长跳转
 */
#pragma mark 擅长跳转
-(void)longSelect
{
    CZQLongViewController *longVC = [CZQLongViewController new];
    
    WS(weakSelf);
    
//    if (self.longArrM.count > 0) {
//        self.resumeModel.FavTags = [self.longArrM componentsJoinedByString:@","];
//    }else
//    {
//        self.resumeModel.FavTags = @"";
//    }
    
    longVC.resumeModel = self.resumeModel;
    
    longVC.saveBlock = ^{
        [weakSelf createSubView];
    };
    
    [self.navigationController pushViewController:longVC animated:YES];
    
}
#pragma mark 保存按钮点击
-(void)sureBtnClick{
    
    
    NSString *nickName = self.myInfoView.nameInputView.titleTF.text;
    
    if ([nickName isEmptyString]) {
        
        [MBProgressHUD showText:@"请输入用户名" toview:self.view];
        
        return;
    }
    
    if ([self.userAccount.Pic isEmptyString]) {
        
        [MBProgressHUD showText:@"请输入头像" toview:self.view];
        
        return;
    }
    
    if ([self.userAccount.Sex isEmptyString]) {
        
        self.userAccount.Sex = @"0";
    }
    
    
    
    [MBProgressHUD showMessage:@"请求中..." toView:self.view];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    VenueModel *venueModel = [VenueModel venue];
    
    self.userAccount.WxAccount = self.myInfoView.weChatInputView.titleTF.text;
    self.userAccount.Memo = self.myInfoView.talkView.textView.text;
    self.userAccount.FavTags = self.resumeModel.FavTags;
    
    if ([self.userAccount.Address isEmptyString]) {
        
        [MBProgressHUD showText:@"请输入城市" toview:self.view];
        
        return;
    }
    
    if ([self.userAccount.WxAccount isEmptyString]) {
        
        [MBProgressHUD showText:@"请输入微信号" toview:self.view];
        
        return;
    }

//    [params setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"ShopID"] forKey:@"ShopID"];

    [params setObject:self.userAccount.ID forKey:@"ID"];

    [params setValue:self.userAccount.Sex forKey:@"Sex"];

    [params setValue:self.userAccount.Pic forKey:@"Pic"];

    [params setObject:nickName forKey:@"NickName"];
    
    [params setValue:self.userAccount.WxAccount forKey:@"WxAccount"];
    
    [params setValue:self.userAccount.Memo forKey:@"Memo"];
    
    [params setObject:self.userAccount.FavTags forKey:@"FavTags"];
    

    [params setValue:self.userAccount.Address forKey:@"Address"];

    [HTTPTool POSTRaw:Post_Users_Operate parameters:params

              success:^(id responseObject){

                  [MBProgressHUD hideHUDForView:self.view];

                  if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {

                      [MBProgressHUD showText:@"保存成功"];

                      [UserAccount saveAccount:self.userAccount];

                      [NotificationCenter postNotificationName:@"ReloadUser" object:nil];

                      [self.navigationController popViewControllerAnimated:YES];
                  }

              }

              failure:^(NSError *error){

                  [MBProgressHUD hideHUDForView:self.view];
              }];
    
}

@end
